package com.betterjr.modules.customer.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.BetterDateUtils;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.modules.account.entity.CustInfo;
import com.betterjr.modules.account.service.CustAccountService;
import com.betterjr.modules.customer.AbstractBaseMechCustTemDataHandlerAdapter;
import com.betterjr.modules.customer.constants.CustomerConstants;
import com.betterjr.modules.customer.dao.CustMechBusinLicenceMapper;
import com.betterjr.modules.customer.entity.CustMechBusinLicence;
import com.betterjr.modules.customer.entity.CustMechBusinLicenceTmp;
import com.betterjr.modules.document.service.CustFileItemService;
import com.betterjr.modules.temhandler.data.TemDataConstant;
import com.betterjr.modules.temhandler.entity.TemData;
import com.betterjr.modules.temhandler.entity.TemDataBuilder;
import com.betterjr.modules.temhandler.service.TemDataService;

/**
 *
 * @author liuwlBaseService<CustMechBusinLicenceMapper, CustMechBusinLicence>
 *
 */
@Service
public class CustMechBusinLicenceService
        extends AbstractBaseMechCustTemDataHandlerAdapter<CustMechBusinLicenceMapper, CustMechBusinLicence> {
    @Resource
    private CustAccountService accountService;

    @Resource
    private CustMechBusinLicenceTmpService businLicenceTmpService;

    @Resource
    private CustFileItemService fileItemService;

    @Autowired
    private TemDataService temDataService;

    /**
     * 更新公司名称
     * @param anCustNo
     * @param anCustName
     */
    public void saveUpdateCustName(final Long anCustNo, final String anCustName) {
        final CustMechBusinLicence businLicence = findBusinLicenceByCustNo(anCustNo);
        BTAssert.notNull(businLicence, "没有找到相应的营业执照！");
        businLicence.setCustName(anCustName);
        this.updateByPrimaryKeySelective(businLicence);
    }

    /**
     * 营业执照信息-查询详情
     *
     * @param anCustNo
     * @return
     */
    public CustMechBusinLicence findBusinLicenceByCustNo(final Long anCustNo) {
        BTAssert.notNull(anCustNo, "客户编号不允许为空！");

        final List<CustMechBusinLicence> businLicences = this.selectByProperty(CustomerConstants.CUST_NO, anCustNo);
        return Collections3.getFirst(businLicences);
    }

    /**
     * 营业执照信息-查询详情
     *
     * @param anCustNo
     * @return
     */
    public CustMechBusinLicence findBusinLicence(final Long anId) {
        BTAssert.notNull(anId, "客户编号不允许为空！");

        final CustMechBusinLicence businLicence = this.selectByPrimaryKey(anId);
        BTAssert.notNull(businLicence, "没有找到营业执照信息!");

        return businLicence;
    }

    /**
     * 营业执照信息-修改 代录/变更
     *
     * @param anCustMechBusinLicence
     * @return
     */
    public CustMechBusinLicence saveBusinLicence(final CustMechBusinLicenceTmp anBusinLicenceTmp) {
        BTAssert.notNull(anBusinLicenceTmp, "营业执照流水信息不允许为空！");

        final Long custNo = anBusinLicenceTmp.getRefId();
        final CustMechBusinLicence tempCustMechBusinLicence = this.findBusinLicenceByCustNo(custNo);
        BTAssert.notNull(tempCustMechBusinLicence, "没有找到营业执照信息!");

        tempCustMechBusinLicence.initModifyValue(anBusinLicenceTmp);
        this.updateByPrimaryKeySelective(tempCustMechBusinLicence);

        return tempCustMechBusinLicence;
    }

    /**
     * 营业执照信息-添加
     *
     * @param anBusinLicence
     * @return
     */
    public CustMechBusinLicence addBusinLicence(final CustMechBusinLicence anBusinLicence, final Long anCustNo) {
        BTAssert.notNull(anBusinLicence, "营业执照信息不允许为空！");

        final CustInfo custInfo = accountService.selectByPrimaryKey(anCustNo);
        anBusinLicence.initAddValue(anCustNo, custInfo.getCustName(), custInfo.getRegOperId(),
                custInfo.getRegOperName(), custInfo.getOperOrg());
        this.insert(anBusinLicence);

        // 建立初始流水记录
        businLicenceTmpService.addBusinLicenceTmp(anBusinLicence);

        return anBusinLicence;
    }

    @SuppressWarnings("unchecked")
    @Override
    public TemData saveAddOrUpdateTemData(TemData anTemData) {
        // 插入校验
        Map<String, Object> map = QueryTermBuilder.newInstance().put("custNo", anTemData.getCustNo()).build();
        List<TemData> temDataList = temDataService.queryTemData(anTemData.getBusinTableType(), "1,2,3", map, "2", 1,
                10);
        BTAssert.isTrue(Collections3.isEmpty(temDataList), "已经有变更在申请过程中");
        return temDataService.saveAddTemData(anTemData);
    }

    public CustMechBusinLicence saveAddCustMechBusinLicence(CustMechBusinLicence businLicence, String anFileList) {

        BTAssert.notNull(businLicence, "公司基本信息-流水信息 不能为空！");
        BTAssert.notNull(businLicence.getCustNo(), "公司基本信息-流水信息 不能为空！");
        businLicence.saveAddInitValue(UserUtils.getOperatorInfo());
        businLicence.setBatchNo(fileItemService.updateAndDuplicateConflictFileItemInfo(anFileList,
                businLicence.getBatchNo(), UserUtils.getOperatorInfo()));
        businLicence.setCustName(accountService.selectByPrimaryKey(businLicence.getCustNo()).getCustName());
        TemData anTemData = convertToTemData(businLicence);
        saveAddOrUpdateTemData(anTemData);
        return businLicence;

    }

    private TemData convertToTemData(CustMechBusinLicence businLicence) {

        TemData anTemData = TemDataBuilder.newInstance().businTableData(JSONObject.toJSONString(businLicence))
                .businTableId(businLicence.getId() + "").businTableOperorg(businLicence.getOperOrg())
                .businTableType(businLicence.getClass().getName()).laster(TemDataConstant.TEMDATA_LASTER_YES)
                .custNo(businLicence.getCustNo()).custName(businLicence.getCustName())
                .regDate(BetterDateUtils.getNumDate()).regOperid(UserUtils.getOperatorInfo().getId())
                .regOpername(UserUtils.getOperatorInfo().getName()).regTime(BetterDateUtils.getNumTime())
                .businStatus(TemDataConstant.TEMDATA_BUSINSTATUS_INSERTED).build();

        return anTemData;
    }

}